package com.gdougms.controller;

import com.gdougms.entity.Field;
import com.gdougms.entity.HttpMessage;
import com.gdougms.entity.User;
import com.gdougms.service.FieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.security.jgss.HttpCaller;

import java.beans.PropertyEditorSupport;
import java.math.BigDecimal;
import java.util.List;

@Api(description = "体育馆管理系统场地管理接口",tags = {"体育馆管理系统场地管理接口"})
@Slf4j
@RestController
public class FieldController {

    @Autowired
    FieldService fieldService;

    @Autowired
    ObjectFactory<HttpMessage> httpMessageObjectFactory;
    synchronized

    @ApiOperation(value = "所有场地查询",notes = "包括维护中的场地")
    @GetMapping("/fieldList")
    public HttpMessage getFieldList(){
        HttpMessage httpMessage = httpMessageObjectFactory.getObject();
        List<Field> fields = fieldService.fieldList();
        if (fields != null){
            httpMessage.setResult(true);
            httpMessage.setObject(fields);
            httpMessage.setCode(200);
            httpMessage.setMessage("获取场地信息成功");
        }else {
            httpMessage.setResult(false);
            httpMessage.setCode(-1);
            httpMessage.setMessage("系统异常，获取场地信息失败！");
        }
        return httpMessage;
    }

    @ApiOperation(value = "删除场地",notes = "根据场地id")
    @GetMapping("/deleteField")
    public HttpMessage deleteFieldById(@RequestParam Integer id){

        int result = fieldService.deleteFieldById(id);

        HttpMessage httpMessage = httpMessageObjectFactory.getObject();
        if (result == 0){
            httpMessage.setMessage("系统异常，删除失败");
            httpMessage.setCode(-1);
            httpMessage.setResult(false);
        }else {
            httpMessage.setMessage("删除成功！");
            httpMessage.setCode(200);
            httpMessage.setResult(true);
        }
        return httpMessage;
    }

    @ApiOperation(value = "更新场地",notes = "根据场地id，id必填，其他一个以上")
    @PostMapping("/updateField")
    public HttpMessage updateField(@RequestParam(required = false) String name,
                                   @RequestParam(required = false) String area,
                                   @RequestParam(required = false)BigDecimal cost,
                                   @RequestParam(required = false) Integer state,
                                   @RequestParam() Integer id){
        int result = fieldService.updateFieldStateById(name, area, cost, id,state);

        HttpMessage httpMessage = httpMessageObjectFactory.getObject();
        if (result == 0){
            httpMessage.setMessage("系统异常，修改失败");
            httpMessage.setCode(-1);
            httpMessage.setResult(false);
        }else {
            httpMessage.setMessage("修改成功！");
            httpMessage.setCode(200);
            httpMessage.setResult(true);
        }
        return httpMessage;
    }

    @ApiOperation(value = "增加场地",notes = "三个参数，费用cost，区域area，名字name")
    @PostMapping("/addField")
    public HttpMessage addField(@RequestParam(required = false) String name,
                                   @RequestParam(required = false) String area,
                                   @RequestParam(required = false)BigDecimal cost){
        int result = fieldService.addField(name, area, cost);

        HttpMessage httpMessage = httpMessageObjectFactory.getObject();
        if (result == 0){
            httpMessage.setMessage("系统异常，添加失败");
            httpMessage.setCode(-1);
            httpMessage.setResult(false);
        }else {
            httpMessage.setMessage("添加成功！");
            httpMessage.setCode(200);
            httpMessage.setResult(true);
        }
        return httpMessage;
    }

    @ApiOperation(value = "获取场地中的所有区域名",notes = "无参数")
    @GetMapping("/getAllArea")
    public HttpMessage getAllArea(){
        HttpMessage  httpMessage = httpMessageObjectFactory.getObject();
        List<String> areas = fieldService.getAllArea();

        if (areas != null){
            httpMessage.setResult(true);
            httpMessage.setMessage("获取Area列成功");
            httpMessage.setObject(areas);
            httpMessage.setCode(200);
        }else {
            httpMessage.setResult(false);
            httpMessage.setMessage("获取Area列失败");
            httpMessage.setCode(-1);
        }
        return httpMessage;
    }

    @ApiOperation(value = "获取场地中的所有花费",notes = "无参数")
    @GetMapping("/getAllCost")
    public HttpMessage getAllCost(){
        HttpMessage  httpMessage = httpMessageObjectFactory.getObject();
        List<BigDecimal> costs = fieldService.getAllCost();

        if (costs != null){
            httpMessage.setResult(true);
            httpMessage.setMessage("获取Cost列成功");
            httpMessage.setObject(costs);
            httpMessage.setCode(200);
        }else {
            httpMessage.setResult(false);
            httpMessage.setMessage("获取Cost列失败");
            httpMessage.setCode(-1);
        }
        return httpMessage;
    }

    @ApiOperation(value = "根据Area获取场地" ,notes = "get传一个area参数就行")
    @GetMapping("/getAllFieldByArea")
    public HttpMessage getAllFieldByArea(@RequestParam String area){
        HttpMessage  httpMessage = httpMessageObjectFactory.getObject();
        List<Field> fields = fieldService.getAllFieldByArea(area);

        if (fields != null){
            httpMessage.setResult(true);
            httpMessage.setMessage("根据area获取fields成功");
            httpMessage.setObject(fields);
            httpMessage.setCode(200);
        }else {
            httpMessage.setResult(false);
            httpMessage.setMessage("根据area获取fields失败");
            httpMessage.setCode(-1);
        }
        return httpMessage;
    }

    @ApiOperation(value = "根据Cost获取场地" ,notes = "get传一个cost参数就行")
    @GetMapping("/getAllFieldByCost")
    public HttpMessage getAllFieldByArea(@RequestParam BigDecimal cost){
        HttpMessage  httpMessage = httpMessageObjectFactory.getObject();
        List<Field> fields = fieldService.getAllFieldByCost(cost);

        if (fields != null){
            httpMessage.setResult(true);
            httpMessage.setMessage("根据cost获取fields成功");
            httpMessage.setObject(fields);
            httpMessage.setCode(200);
        }else {
            httpMessage.setResult(false);
            httpMessage.setMessage("根据cost获取fields失败");
            httpMessage.setCode(-1);
        }
        return httpMessage;
    }


}
